
import { Component } from "./component"
import { Transform } from "./index";



//js创建go时,在rust处也创建一个go,rust存储全部的go,js保持go的映射,通过name
//when creating gameobject with js,also creating a gameobject in rust side
//rust stores all gameobjects and js contain the mapper of gameobject through name
export class GameObject {

    transform: Transform;

    //不能使用自带的构造方法
    //can't use the constructor method below
    constructor(private name: string) { }


    static create(name: string): GameObject {
        return Deno.core.ops.op_createGameObject(name);
    }


    addComponent(value: Component) {
        let type: any = value.constructor;
        Deno.core.ops.op_addComponent(this.name, value, type.typeName);
    }

    getComponent<T extends typeof Component>(value: T): InstanceType<T> {
        return Deno.core.ops.op_getComponent(this.name, value.typeName);
    }

    getComponentById<T extends Component>(objId: any, compId: any): T {
        return Deno.core.ops.op_getComponentById(objId, compId);
    }

}


export function getGameObject(name: string): GameObject {
    return Deno.core.ops.op_getGameObject(this, name);
}

export function getGameObjectById(id: any): GameObject {
    return Deno.core.ops.op_getGameObjectById(this, id);
}

export function getComponentById<T extends Component>(objId: any, compId: any): T {
    return Deno.core.ops.op_getComponentById(objId, compId);
}


globalThis.__GAMEOBJECT__ = new GameObject("");